package com.wl.shop.service.impl;

import com.wl.shop.entity.Dict;
import com.wl.shop.dao.DictDao;
import com.wl.shop.service.DictService;
import com.wl.shop.utils.DictUtils;
import com.wl.shop.utils.StringUtils;
import com.wl.shop.utils.UserUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author wangl
 * @since 2017-06-08
 */
@Service("dictService")
@Transactional(readOnly = true)
public class DictServiceImpl extends ServiceImpl<DictDao, Dict> implements DictService {

	@Autowired
	private DictDao dictDao;
	@Override
	public int getMaxSort(String type) {
		// TODO Auto-generated method stub
		Integer count = dictDao.selectMaxSort(type);
		if(count !=null && count != 0){
			return count;
		}else{
			return 0;
		}
	}
	@Override
	public void saveOrUpdate(Dict dict) {
		// TODO Auto-generated method stub
		if(StringUtils.isBlank(dict.getId())){
			dict.preInsert();
			insert(dict);
		}else{
			dict.preUpdate();
			updateById(dict);
		}
		// 清除字典缓存
		UserUtils.removeCache(DictUtils.CACHE_DICT_MAP);
	}
	@Override
	public Boolean isSameData(Dict dict) {
		// TODO Auto-generated method stub
		List<Dict> list = selectList(new EntityWrapper<Dict>(dict));
		if(list !=null && list.size()>0){
			return true;
		}else{
			return false;
		}
	}
	
}
